package com.sumansoul.photodemo;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;

import com.qw.soul.permission.SoulPermission;
import com.qw.soul.permission.bean.Permission;
import com.qw.soul.permission.bean.Permissions;
import com.qw.soul.permission.bean.Special;
import com.qw.soul.permission.callbcak.CheckRequestPermissionListener;
import com.qw.soul.permission.callbcak.CheckRequestPermissionsListener;
import com.qw.soul.permission.callbcak.GoAppDetailCallBack;
import com.qw.soul.permission.callbcak.SpecialPermissionListener;
import com.sumansoul.photodemo.support.SettingUtil;


/**
 * @author LYY
 * 权限申请Utils
 */
public class AppPermissionUtils {


    private static CommonInterface.OnPermisssionFinish onPermisssionFinish;


    /**
     * 单个公共权限
     * @param context
     */
    public static void requestCommonSinglePermission(final Context context, CommonInterface.OnPermisssionFinish onPermisssionFinish) {
        SoulPermission.getInstance().checkAndRequestPermission(Manifest.permission.ACCESS_FINE_LOCATION,
                //if you want do noting or no need all the callbacks you may use SimplePermissionAdapter instead
                new CheckRequestPermissionListener() {
                    @Override
                    public void onPermissionOk(Permission permission) {
                      //  PeremissionUtils.showMessage(view, permission.toString() + "\n is ok , you can do your operations");
                        //权限完成
                      if (onPermisssionFinish!=null){
                          onPermisssionFinish.onPermisssionFinish();
                      }

                    }

                    @Override
                    public void onPermissionDenied(Permission permission) {
                        //PeremissionUtils.showMessage(context, permission.toString() + " \n is refused you can not do next things");

                        //单个权限申请返回结果
                        new AlertDialog.Builder(context)
                                .setTitle("提示")
                                .setMessage("请允许权限")
                                .setPositiveButton("去设置", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        SettingUtil.go2Setting(context);
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                })
                                .create().show();

                    }
                });
    }



    /**
     * 多个公共权限
     * @param context
     * @param onPermisssionFinish
     */
    public static void requestCommonPermissions(final Context context, CommonInterface.OnPermisssionFinish onPermisssionFinish, String... permissions) {
        SoulPermission.getInstance().checkAndRequestPermissions(
                Permissions.build(permissions),
                //if you want do noting or no need all the callbacks you may use SimplePermissionsAdapter instead
                new CheckRequestPermissionsListener() {
                    @Override
                    public void onAllPermissionOk(Permission[] allPermissions) {
                        //PeremissionUtils.showMessage(view, allPermissions.length + "permissions is ok" + " \n  you can do your operations");
                        //权限完成
                        if (onPermisssionFinish!=null){
                            onPermisssionFinish.onPermisssionFinish();
                        }
                    }

                    @Override
                    public void onPermissionDenied(Permission[] refusedPermissions) {
                        //PeremissionUtils.showMessage(context, refusedPermissions[0].toString() + " \n is refused you can not do next things");
                        new AlertDialog.Builder(context)
                                .setTitle("提示")
                                .setMessage("请允许权限")
                                .setPositiveButton("去设置", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        SettingUtil.go2Setting(context);
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                })
                                .create().show();


                    }
                });
    }

    /**
     * 检查单个权限
     * @param context
     */
    public static void checkSinglePermission(Context context) {
        //you can also use checkPermissions() for a series of permissions
        Permission checkResult = SoulPermission.getInstance().checkSinglePermission(Manifest.permission.ACCESS_FINE_LOCATION);
        PeremissionUtils.showMessage(context, checkResult.toString());
    }


    public static void requestSinglePermission(final Context context, String SinglePermission) {
        SoulPermission.getInstance().checkAndRequestPermission(SinglePermission,
                //if you want do noting or no need all the callbacks you may use SimplePermissionAdapter instead
                new CheckRequestPermissionListener() {
                    @Override
                    public void onPermissionOk(Permission permission) {
                        PeremissionUtils.showMessage(context, permission.toString() + "\n is ok , you can do your operations");
                    }

                    @Override
                    public void onPermissionDenied(Permission permission) {
                        PeremissionUtils.showMessage(context, permission.toString() + " \n is refused you can not do next things");
                    }
                });
    }

    public static void requestPermissions(final Context context) {
        SoulPermission.getInstance().checkAndRequestPermissions(
                Permissions.build(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE),
                //if you want do noting or no need all the callbacks you may use SimplePermissionsAdapter instead
                new CheckRequestPermissionsListener() {
                    @Override
                    public void onAllPermissionOk(Permission[] allPermissions) {
                        PeremissionUtils.showMessage(context, allPermissions.length + "permissions is ok" + " \n  you can do your operations");
                    }

                    @Override
                    public void onPermissionDenied(Permission[] refusedPermissions) {
                        PeremissionUtils.showMessage(context, refusedPermissions[0].toString() + " \n is refused you can not do next things");
                    }
                });
    }

    public static void requestSinglePermissionWithRationale(final Context context) {
        SoulPermission.getInstance().checkAndRequestPermission(Manifest.permission.READ_CONTACTS,
                new CheckRequestPermissionListener() {
                    @Override
                    public void onPermissionOk(Permission permission) {
                        PeremissionUtils.showMessage(context, permission.toString() + "\n is ok , you can do your operations");
                    }

                    @Override
                    public void onPermissionDenied(Permission permission) {
                        // see CheckPermissionWithRationaleAdapter
                        if (permission.shouldRationale()) {
                            PeremissionUtils.showMessage(context, permission.toString() + " \n you should show a explain for user then retry ");
                        } else {
                            PeremissionUtils.showMessage(context, permission.toString() + " \n is refused you can not do next things");
                        }
                    }
                });
    }




    public static void checkAndRequestUnKnownSource(final View view) {
        //if you want do noting or no need all the callbacks you may use SimpleSpecialPermissionAdapter instead
        SoulPermission.getInstance().checkAndRequestPermission(Special.UNKNOWN_APP_SOURCES, new SpecialPermissionListener() {
            @Override
            public void onGranted(Special permission) {
                PeremissionUtils.showMessage(view, "install unKnown app  is enable now ");
            }

            @Override
            public void onDenied(Special permission) {
                PeremissionUtils.showMessage(view, "install unKnown app  is disable yet");
            }
        });
    }

    public static void goApplicationSettings(final View view) {
        SoulPermission.getInstance().goApplicationSettings(new GoAppDetailCallBack() {
            @Override
            public void onBackFromAppDetail(Intent data) {
                //if you need to know when back from app detail
                PeremissionUtils.showMessage(view, "back from go appDetail");
            }
        });
    }

    public static void getTopActivity(View view) {
        Activity activity = SoulPermission.getInstance().getTopActivity();
        if (null != activity) {
            PeremissionUtils.showMessage(view, activity.getClass().getSimpleName() + " " + activity.hashCode());
        }
    }

    public void setOnVideoCompletion(CommonInterface.OnPermisssionFinish onPermisssionFinish){
        AppPermissionUtils.onPermisssionFinish = onPermisssionFinish;
    }
}
